import Prop from "./Prop";
import ShootManager from "./ShootManager";

/**
*
* @ author:JiangChencheng
* @ email:744578095@qq.com
* @ data: 2021-10-29 11:14
*/
export default class GameManager extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:scoreTxt, tips:"成绩文本", type:Node, default:null}*/
        this.scoreTxt = null
        
        this.signArr = ["Jia","Triangle","Circle","Square"]
        this.rowID = 0
        this.prefabArr = new Array()
        this.shootManager = null
        this.score = 0
    }

    onAwake() {

        this.scoreTxt.text = this.score
        this.shootManager = this.owner.getComponent(ShootManager)
        var resArr = [
            {url:"prefab/jia.json", type:Laya.Loader.PREFAB},
            {url:"prefab/circle.json", type:Laya.Loader.PREFAB},
            {url:"prefab/square.json", type:Laya.Loader.PREFAB},
            {url:"prefab/triangle.json", type:Laya.Loader.PREFAB}
        ]
        Laya.loader.load(resArr,new Laya.Handler(this,function(){
            resArr.forEach(element => {
                this.prefabArr.push(Laya.loader.getRes(element.url))
            });
            this.createRow()
        }))

        Laya.stage.on("CreateNextRow",this,this.createRow)
        Laya.stage.on("AddScore",this,this.addScore)
        
    }

    onDestroy(){
        Laya.stage.off("CreateNextRow",this,this.createRow)
    }

    addScore(damage){
        this.score+=damage
        this.scoreTxt.text = this.score
    }

    // 生成每行的元素
    createRow(){
        this.propMoveUp()
        // 可以生成元素的x坐标位置
        var xArr = [180,300,420,540,660,780,900]
        // 随机元素数量
        var count = this.getRandom(1,4)
        for(var i = 0; i < count; i++){
            var index = this.getRandom(0,this.prefabArr.length-1)
            var sign = this.signArr[index]
            // var element = this.prefabArr[index].create()
            var element = Laya.Pool.getItemByCreateFun(sign,function(){
                return this.prefabArr[index].create()
            },this)
            element.visible = true
            element.active = true
            this.owner.addChild(element)
            var xIndex = this.getRandom(0,xArr.length-1)
            var x = xArr[xIndex]
            xArr.splice(xIndex,1)

            element.pos(x,1500)
            if (element.getComponent(Prop) != null) {
                var shootCount = this.getRandom(this.shootManager.canShootCount,this.shootManager.canShootCount*3)
                element.getComponent(Prop).Init(shootCount,this.rowID,sign)
            }
     
        }
        this.rowID++
    }

    // 生成元素前让之前的元素上升一格
    propMoveUp(){
        for (var i = 0; i < this.owner.numChildren; i++) {
            var child = this.owner.getChildAt(i)
            if (child.active == false) continue
            if (child.getComponent(Prop) != null) {
                child.y-=120
            }
        }
    }

    getRandom(min,max) {
        var value = (max - min)*Math.random()
        value = Math.round(value)
        return value + min
    }       

}